package org.lanqiao.tjut.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户身份登录验证过滤器
 */
public class LoginedCheckedFilter implements Filter {

	public LoginedCheckedFilter() {
	}

	public void destroy() {
	}

	/**
	 * 登录身份验证的过滤器
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 验证session缓存中有没有缓存的用户信息 loginedAdminB
		// 获取httpservletrequest对象
		HttpServletRequest httpReq = (HttpServletRequest) request;
		HttpServletResponse httpResp = (HttpServletResponse) response;
		Object objLoginedB = httpReq.getSession().getAttribute("loginedAdminB");
		// 通过判定是否缓存了用户信息，来判定用户是否进行了登录验证
		if (objLoginedB == null) {
			// 没有进行登录验证，将其转发到登录页面
			httpResp.sendRedirect("../login.jsp");
		}
		// 进行了登录验证
		// 继续后续的处理
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
